 var ExtendJson = null;

 var path = require('path');

 var fs = require('fs');

 module.exports = function (UC, source, config, ArFor, cache, package) {
     var jmodels = config.jmodels;
     config.InitExtendJson = function (config) {
         ExtendJson = null;
         config();
     }
     var source = config.Source;
     var UserControl = UC;
     var DeletNotJurisdictions = function (db, Table) {
         if (db == null) {
             return null;
         } else if (db.jurisdiction) {
             db.jurisdiction = Table;
             return db;
         }
         var deleteitem = function (name, List, models, _List, _models) {
             if (List) {
                 for (var key in List) {
                     var item = List[key];
                     if (item.__id && (item.__id.toLocaleLowerCase() == name || item.__id.toLocaleLowerCase() == 'sys')) {
                         _List[key] = item;
                         if (item.modelID || item.model) {
                             var model = item.model || models[item.modelID]
                             if (model && item.modelID) {
                                 _models[item.modelID] = model;
                             }
                             if (model.condition) {
                                 return item;
                             } else {
                                 return false
                             }
                         } else if (item.Menu && item.Menu.List) {
                             return item;
                         } else {

                             return false
                         }
                     }
                 }
             }
             return false
         }
         var getnodes = function (key, List, models, _List, _models) {
             var node = deleteitem(key, List, models, _List, _models);
             if (rows.subs && rows.subs.length > 0 && List && List.search && List.search.__id == key && List.search.List) {
                 var __List = [];
                 for (var b = 0; b < rows.subs.length; b++) {
                     var row = rows.subs[b];
                     var kem = row.keys;
                     for (var x = 0; x < List.search.List.length; x++) {
                         var objx = List.search.List[x];
                         if (objx.__id == kem) {
                             __List.push(objx);
                         }
                     }
                 }
                 if (__List.length > 0) {
                     _List.search.List = __List;
                 } else {
                     delete _List.search;
                 }
             } else if (node && rows.subs) {
                 if (node.Menu && node.Menu.List) {
                     node.Menu.List = DeletNotJurisdictions(node.Menu.List, rows.subs);
                 }
             } else if (node && rows.subs) {
                 if (node.modelID) {
                     var _key = key;
                     for (var k in List) {
                         if (List[k].__id.toLocaleLowerCase() == key) {
                             _key = List[k].modelID;
                         };
                     };
                     var model = _models[_key];
                     model.condition = DeletNotJurisdictions(model.condition, rows.subs);
                     _models[_key] = model;
                 } else {
                     node.model.condition = DeletNotJurisdictions(node.model.condition, rows.subs);
                 }
             };
         }

         var List = db.List;
         if (db.Menu) {
             var List = db.Menu.List;
         }
         var operations = db.operations;
         var models = db.models;
         var _List = {};
         var _operations = {};
         var _models = {};

         for (var i = 0; i < Table.length; i++) {
             var rows = Table[i];
             var key = rows.keys;
             if (typeof (key) == "string" && key != "") {
                 key = key.toLocaleLowerCase();
                 getnodes(key, List, models, _List, _models);
                 getnodes(key, operations, models, _operations, _models);
             };
         };
         if (db.public) {

         } else {
             if (db.Menu) {
                 db.Menu.List = _List;
             } else if (db.List) {
                 db.List = _List;
             }
             if (db.operations) {
                 db.operations = _operations;
             };
             if (db.models) {
                 db.models = _models;
             };
         }
         if (db.Menu == null && (db.name == "Table" || db.name == "cards")) {
             if (db.List || db.search) {
                 db.Menu = {
                     List: db.List
                 };
                 delete db.List;
             }
         };

         return db;
     };
     var getextend = function (id, Callback) {


         jmodels.open(id, function (jmodel) {


             Callback(jmodel);

         })
     };
     var routerjson = function (req, res, val) {
         res.Json(val);
     }
     var router = function (req, res, next) {
         if (UserControl.getoff(req.session.key)) {
             var body = req.body;
             var id = 'i_' + body.mid;
             getextend(id, function (jmodel) {
                 if (jmodel) {

                    body = UserControl.GetForm(req.session.key, body);
                     var etag = config.getETag(Object.assign({
                        user_roles:body.user_roles_id,
                         package_id: jmodel.package('id'),
                         package_version: jmodel.package('version'),
                         url: config.getdomain(req)
                     }, req.body)); 
                     if (req.getHeader('if-none-match') == etag) {
                             req.notModified();
                             return;
                     }
                     res.etag_key = etag;
    
                     new source("M_679", body, function (obj) {
                         var jsonstr = JSON.stringify(jmodel);
                         var cb = JSON.parse(jsonstr);
                         cb = DeletNotJurisdictions(cb, obj.Table);
                         res.Json(cb);
                     })
                 } else {
                     routerjson(req, res, {
                         error: 2
                     })
                 }
             });
         } else {
             routerjson(req, res, {
                 error: 1
             })
         }
     }
     return router;
 }